﻿namespace PMS.Client.FrmOrgan
{
    using AsyncFactory;
    using PMS.BLL;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class FrmSelect : Form
    {
        private Button btnCancel;
        private Button btnOK;
        private IContainer components = null;
        private GroupBox groupBox1;
        private Label label1;
        private OrganBLL organBLL = new OrganBLL();
        private List<PMS.Model.Organ> organList = null;
        private TreeView treeView1;
        private TextBox txtSelect;

        public FrmSelect()
        {
            this.InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode != null)
            {
                this.txtSelect.Text = this.treeView1.SelectedNode.Text;
                this.OrganName = this.treeView1.SelectedNode.Text;
                base.DialogResult = DialogResult.OK;
                base.Close();
            }
            else
            {
                MessageBox.Show("请选择");
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmMoveOut_Load(object sender, EventArgs e)
        {
            this.organList = null;
            AsyncProcess.Process(() => this.organList = this.organBLL.GetList(), delegate {
                this.ReturnOrganBind(this.organList, 0, this.treeView1.Nodes);
                if (this.treeView1.Nodes.Count > 0)
                {
                    this.treeView1.Nodes[0].Expand();
                }
            }, false, LoadingImageType.Strip);
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FrmSelect));
            this.treeView1 = new TreeView();
            this.label1 = new Label();
            this.txtSelect = new TextBox();
            this.groupBox1 = new GroupBox();
            this.btnOK = new Button();
            this.btnCancel = new Button();
            this.groupBox1.SuspendLayout();
            base.SuspendLayout();
            this.treeView1.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            this.treeView1.BorderStyle = BorderStyle.FixedSingle;
            this.treeView1.Dock = DockStyle.Top;
            this.treeView1.FullRowSelect = true;
            this.treeView1.HideSelection = false;
            this.treeView1.ItemHeight = 0x10;
            this.treeView1.Location = new Point(0, 0);
            this.treeView1.Name = "treeView1";
            this.treeView1.PathSeparator = " -> ";
            this.treeView1.Size = new Size(0x160, 0x183);
            this.treeView1.TabIndex = 1;
            this.treeView1.AfterSelect += new TreeViewEventHandler(this.treeView1_AfterSelect);
            this.label1.AutoSize = true;
            this.label1.Location = new Point(12, 0x11);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "您选择的是";
            this.txtSelect.BackColor = Color.FromArgb(0xd9, 0xea, 0xf7);
            this.txtSelect.BorderStyle = BorderStyle.FixedSingle;
            this.txtSelect.Location = new Point(0x53, 15);
            this.txtSelect.Name = "txtSelect";
            this.txtSelect.Size = new Size(0x101, 0x15);
            this.txtSelect.TabIndex = 50;
            this.groupBox1.Controls.Add(this.txtSelect);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Location = new Point(0, 0x183);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0x160, 0x2e);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.btnOK.BackColor = Color.White;
            this.btnOK.FlatStyle = FlatStyle.Popup;
            this.btnOK.Location = new Point(0x53, 0x1b7);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x4b, 0x17);
            this.btnOK.TabIndex = 8;
            this.btnOK.Text = "确定";
            this.btnOK.UseVisualStyleBackColor = false;
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            this.btnCancel.BackColor = Color.White;
            this.btnCancel.FlatStyle = FlatStyle.Popup;
            this.btnCancel.Location = new Point(0x109, 0x1b7);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x4b, 0x17);
            this.btnCancel.TabIndex = 9;
            this.btnCancel.Text = "关闭";
            this.btnCancel.UseVisualStyleBackColor = false;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            this.BackColor = Color.FromArgb(0xa4, 0xc4, 0xeb);
            base.ClientSize = new Size(0x160, 0x1dc);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.btnOK);
            base.Controls.Add(this.groupBox1);
            base.Controls.Add(this.treeView1);
            base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            base.Name = "FrmSelect";
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "选择机构";
            base.Load += new EventHandler(this.FrmMoveOut_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            base.ResumeLayout(false);
        }

        private void ReturnOrganBind(List<PMS.Model.Organ> organList, int OrganID, TreeNodeCollection treeNodes)
        {
            if (organList == null)
            {
                organList = new List<PMS.Model.Organ>();
            }
            List<PMS.Model.Organ> list = organList.FindAll(delegate (PMS.Model.Organ f) {
                int? parentID = f.ParentID;
                int organID = OrganID;
                return (parentID.GetValueOrDefault() == organID) && parentID.HasValue;
            }).ToList<PMS.Model.Organ>();
            if (list.Count > 0)
            {
                TreeNode node = null;
                foreach (PMS.Model.Organ organ in list)
                {
                    node = new TreeNode {
                        Text = organ.OrganName,
                        ImageIndex = 0,
                        Name = organ.OrganID.ToString(),
                        Tag = organ.LevelNo
                    };
                    treeNodes.Add(node);
                    this.ReturnOrganBind(organList, organ.OrganID, node.Nodes);
                }
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.txtSelect.Text = this.treeView1.SelectedNode.Text;
            this.OrganName = this.treeView1.SelectedNode.Text;
            this.Organ = this.organList.FirstOrDefault<PMS.Model.Organ>(f => f.OrganID == int.Parse(this.treeView1.SelectedNode.Name));
            this.FullPathName = this.treeView1.SelectedNode.FullPath;
        }

        public string FullPathName { get; set; }

        public PMS.Model.Organ Organ { get; set; }

        public string OrganName { get; set; }
    }
}

